--[[
ChatTime

Updated from ChatTimestamp by Micah, a previous chat time mod.

This expands on the feature set and also updates the mod for the current release of WoW.

Wishlist
* Only function on specific chat frames

]]


-- Program Start
local Original_ChatFrame_OnEvent;

------------------------------------------------------------------------------------------------
-- Core Program Functions - this program can't run without these.
------------------------------------------------------------------------------------------------
function LysChatTime_OnLoad(self)
	-- Register the variables loaded event...
	self:RegisterEvent("VARIABLES_LOADED");

	-- Register our slash commands
	SLASH_CHATTIME0 = "/chattime";
	SLASH_CHATTIME1 = "/ct";
	SLASH_CHATTIME2 = "/lct";
	SlashCmdList["CHATTIME"] = function(msg)
		LysChatTime_SlashCommandHandler(msg);
	end

	-- Hook ChatFrame_OnEvent so we can hook AddMessage
	hooksecurefunc("ChatFrame_OnEvent", LysChatTime_ChatFrame_OnEvent);
end

function LysChatTime_OnEvent(self,event,...)
	if (event == "VARIABLES_LOADED") then
		if ((not LysChatTime_Config) or (LysChatTime_Config.Style)) then
			LysChatTime_Config	= {};
		end

		LysChatTime_PLAYER = LCT_Shared_GetPlayerRealmName();

		if (not LysChatTime_Config[LysChatTime_PLAYER]) then
			LysChatTime_Config[LysChatTime_PLAYER] 			= {};
			LysChatTime_Config[LysChatTime_PLAYER].Offset		= "0";
			LysChatTime_Config[LysChatTime_PLAYER].Style		= "24";
			LysChatTime_Config[LysChatTime_PLAYER].Seconds	= false;
		end

		-- Enable chat time functions
		LysChatTime_ENABLE = true;

		if (LCT_Shared_VERS_NUM ~= nil) then
			-- Program loaded messages
			LCT_Shared_ProgramLoadedText(LysChatTime_VERSION);			
		else
			DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_VERSION.." loaded.");
			UIErrorsFrame:AddMessage(LysChatTime_VERSION.." loaded.", 1.0, 1.0, 1.0, 1.0, UIERRORS_HOLD_TIME);
		end
	end
end

function LysChatTime_ChatFrame_OnEvent(self,event,...)
	--call the real ChatFrame_OnEvent function
	--Original_ChatFrame_OnEvent(event);

	if (LysChatTime_ENABLE) then
		if (not self.Original_AddMessage) then
			self.Original_AddMessage = self.AddMessage;
			self.AddMessage = LysChatTime_AddMessage;
		end
	end
end

------------------------------------------------------------------------------------------------
-- Internal program functions
------------------------------------------------------------------------------------------------
local function LysChatTime_CheckHour(hour)
	if (hour >= 24) then
		hour = hour - 24;
	elseif (hour < 0) then
		hour = hour + 24;
	end
	
	return hour;
end

local function LysChatTime_Display_Time()
	local TimeDisplay;
	local myhour;
	local SecondsDisplay = "";
	local hour, minute = GetGameTime();
	-- local hour	= string.sub(date(),  10, 11)
	-- local minute	= string.sub(date(),  13, 14)
	local seconds	= string.sub(date(),  16, 17)

	myhour = hour + LysChatTime_Config[LysChatTime_PLAYER].Offset;
	hour = LysChatTime_CheckHour(myhour);
	
	if (LysChatTime_Config[LysChatTime_PLAYER].Seconds) then
		SecondsDisplay = format(":%02d", seconds);
	end
	
	if (LysChatTime_Config[LysChatTime_PLAYER].Style == "24") then
		TimeDisplay = format("[%02d:%02d%s]", hour, minute, SecondsDisplay);
	else
		if(hour >= 12) then
			if (hour > 12) then
				hour = (hour - 12);
			end
			TimeDisplay = format("[%02d:%02d%s %s]", hour, minute, SecondsDisplay, LysChatTime_PM);
		else
			TimeDisplay = format("[%02d:%02d%s %s]", hour, minute, SecondsDisplay, LysChatTime_AM);
		end
	end
	
	return TimeDisplay;
end	

function LysChatTime_AddMessage(self, msg, r, g, b, id)
	local ChatLogTime = LysChatTime_Display_Time();
	local newmsg;
	
	-- If actually given a string to display, then format it correctly, otherwise just ignore the input as the blizzard handler does.
	if (msg ~= nil) then
		newmsg = format("%s %s", ChatLogTime, msg);
		self:Original_AddMessage(newmsg, r, g, b, id); --call the real AddMessage function
	end
end

function LysChatTime_SlashCommandHandler(msg)
	msg = string.lower(msg);

	if (msg == '') then msg = nil end

	if (msg) then
		local command = LCT_Shared_Split(msg, "=");
		local current = string.lower(command[1]);

		if (current == LysChatTime_CMD_FORMAT) then
			if (command[2] == "24") then
				LysChatTime_Config[LysChatTime_PLAYER].Style = "24";
				DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_CHANGE_FORMAT24);
				return;
			elseif (command[2] == "12") then
				LysChatTime_Config[LysChatTime_PLAYER].Style = "12";
				DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_CHANGE_FORMAT12);
				return;
			else
				DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_ERROR_INVALID_FORMAT);
				return;
			end
		elseif (current == LysChatTime_CMD_OFFSET) then
			local myOffset = tonumber(command[2]);
			if ((myOffset < 12) and (myOffset > -12)) then
				LysChatTime_Config[LysChatTime_PLAYER].Offset = myOffset;
				DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_CHANGE_OFFSET..LysChatTime_Config[LysChatTime_PLAYER].Offset);
				return;
			else
				DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_ERROR_INVALID_OFFSET);
				return;
			end
		elseif (current == LysChatTime_CMD_SECONDS) then
			if (LysChatTime_Config[LysChatTime_PLAYER].Seconds) then
				LysChatTime_Config[LysChatTime_PLAYER].Seconds = false;
				DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_CHANGE_SECONDS);
				return;
			else
				LysChatTime_Config[LysChatTime_PLAYER].Seconds = true;
				DEFAULT_CHAT_FRAME:AddMessage(LysChatTime_CHANGE_SECONDS);
				return;
			end
		end
	end
	LCT_Shared_Display_Help("LysChatTime_HELP_TEXT");
end
